package service

import (
	"awesomeTask/application/client"
	"awesomeTask/application/event"
	"awesomeTask/application/models/master"
	"awesomeTask/application/timer"
	"awesomeTask/application/variable"
	"awesomeTask/application/vote"
	"awesomeTask/commom/MessageHelper"
	event2 "awesomeTask/system/event"
	"fmt"
	"net"
)

type EventService struct {
}

func (ioc EventService) Register() {
	event.Init()
	timer.InitTimer()
	event2.Register("runAsNode", func(args ...interface{}) {
		ClientTimerService{}.Register()
		client.BindEvent()
		timer.SetTimeInterval(func() {
			master.GetMasterObj().FindMaster()
		}, 20)
		event2.Dispatch(variable.ON_TRY_KEEPALIVE)
	})
	event2.Register(variable.ON_MASTER_DISCONNECTED, func() {
		vote.VoteHandler{}.ResetVotes() //重置选票
	})
	event2.Register(variable.ON_CALLBACK_EVENT, func(args ...interface{}) {
		eventInfo := args[0].(map[string]interface{})
		realEvent := eventInfo["realEvent"].(string)
		callBackEvent := eventInfo["callBackEvent"].(string)
		fmt.Println(args)
		res := event2.SyncDispatch(realEvent, map[string]interface{}{"taskInfo": args[0]})
		conn := eventInfo["connection"].(net.Conn)
		resEventInfo := map[string]interface{}{
			"result": res,
		}
		//if len(callBackEvent)==len("ON_TASK_REMOTE_FORK_TO_MASTER_7837458415"){
		//	panic("debug")
		//}
		MessageHelper.SendEvent(callBackEvent, resEventInfo, conn)
	})

}
func (ioc EventService) Boot() {

}
